package com.medical.medical.controller.yhzController;

import com.medical.medical.entity.Patient;
import com.medical.medical.service.PatientService;
import com.medical.medical.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @BelongsProject: medical
 * @BelongsPackage: com.medical.medical.controller.yhzController
 * @authorDesc 十年生死两茫茫，写程序，到天亮
 * @Author: 喜杨杨
 * @CreateTime: 2022-09-14  17:41
 * @Description:
 * @Version: 1.0
 */
@Controller
@Slf4j
public class PatientControllerYHZ {
    @Resource
    private PatientService patientService;

    @RequestMapping("addPatient")
    public String add(){
        return "addPatient";
    }


    /**
     * @Description: 用户注册
     * @author: 喜杨杨
     * @date: 2022/9/15 12:31
     * @param: [patient]
     * @return: java.lang.String
     **/
    @RequestMapping("addPatient.do")
    public String addPatient(Patient patient){
        //由身份证号计算年龄
        String idCard = patient.getIDCard();
        //截取身份证号的6到10位
        String age = idCard.substring(6, 10);
        //转换为int类型
        int newage = Integer.parseInt(age);
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        String format = formatter.format(date);
        int anInt = Integer.parseInt(format);
        patient.setAge(anInt-newage);
        //MD5加密
        String pwd=    MD5Util.convertMD5(patient.getPassword());
        patient.setPassword(pwd);
        if(patientService.addPatient(patient)){
            log.info("用户添加成功....");
            return "login";
        }else{
            log.info("用户添加失败....");
            return "addPatient";
        }
    }
}
